﻿Imports ICloseCircle.ClassConsManager
Public Class ClassFileReader
    Private pointArray(,) As String
    Private lineArray(,) As String
    Private consManager As ClassConsManager = New ClassConsManager()
    Private loger As IClassLog = New IClassLog
    ''' <summary>
    ''' 读取边数据，在读取边数据前，先应该形成点数据文件
    ''' </summary>
    ''' <param name="lineDateFileName">边数据文件名称</param>
    ''' <param name="result">边结果数组</param>
    ''' <param name="hasRecordCountHead">是否包含数据格式头，默认不包含</param>
    ''' <remarks> 如果不包含文件头个数，则最多只能有1000条边</remarks>

    Public Sub readLineData(ByVal lineDateFileName As String, ByRef result(,) As String, Optional ByVal hasRecordCountHead As Boolean = False)
        Dim lineFileNo As Integer
        lineFileNo = FreeFile()
        If (Dir(lineDateFileName) = "") Then
            loger.log("边数据文件" & lineDateFileName & "不存在", ClassConsManager.LOG_LEVEL_ERROR)
        End If

        Dim fileReader = My.Computer.FileSystem.OpenTextFileReader(lineDateFileName, System.Text.Encoding.Default)

        Dim lineCount As Integer = 0
        If hasRecordCountHead Then
            lineCount = Val(fileReader.ReadLine())
        Else
            lineCount = 1000
        End If
        ReDim lineArray(lineCount, 10)


        Dim i = 0
        Try

            While (Not fileReader.EndOfStream)
                Dim dataLine As String = fileReader.ReadLine()
                If dataLine = "" Then
                    Continue While
                ElseIf Mid(dataLine, 1, 1) = "#" Then
                    Continue While
                End If

                Dim dataArrat = Split(dataLine, ClassConsManager.FIELD_SPERATE_STRING)

                '边编号
                If ClassConsManager.FILE_LINE_NODE_NO_FIELD = -1 Then
                    lineArray(i, ClassConsManager.LINE_NODE_NO_FIELD) = Trim(Str(i))
                Else
                    lineArray(i, ClassConsManager.LINE_NODE_NO_FIELD) = dataArrat(ClassConsManager.FILE_LINE_NODE_NO_FIELD)
                End If

                '边起点
                If ClassConsManager.FILE_LINE_BEGIN_NODE_FIELD = -1 Then
                    lineArray(i, ClassConsManager.LINE_BEGIN_NODE_FIELD) = Trim(Str(i))
                Else
                    lineArray(i, ClassConsManager.LINE_BEGIN_NODE_FIELD) = dataArrat(ClassConsManager.FILE_LINE_BEGIN_NODE_FIELD)
                End If


                '边终点
                If ClassConsManager.FILE_LINE_END_NODE_FIELD = -1 Then
                    lineArray(i, ClassConsManager.LINE_END_NODE_FIELD) = 0
                Else
                    lineArray(i, ClassConsManager.LINE_END_NODE_FIELD) = dataArrat(ClassConsManager.FILE_LINE_END_NODE_FIELD)
                End If

                ''纬度
                'If ClassConsManager.FILE_NODE_LAIITUDE_FIELD = -1 Then
                '    pointArray(i, ClassConsManager.NODE_LAIITUDE_FIELD) = 0
                'Else
                '    pointArray(i, ClassConsManager.NODE_LAIITUDE_FIELD) = dataArrat(ClassConsManager.FILE_NODE_LAIITUDE_FIELD)
                'End If

                ''高程
                'If ClassConsManager.FILE_NODE_ALTITUDE_FIELD = -1 Then
                '    pointArray(i, ClassConsManager.NODE_ALTITUDE_FIELD) = 0
                'Else
                '    pointArray(i, ClassConsManager.NODE_ALTITUDE_FIELD) = dataArrat(ClassConsManager.FILE_NODE_ALTITUDE_FIELD)
                'End If
                i = i + 1
            End While
            fileReader.Close()
        Catch ex As Exception

        End Try
        '  ReDim Preserve lineArray(i - 1, 10)
        If result Is Nothing Then
            result = lineArray
        End If
        Array.Copy(lineArray, result, (i - 1) * 10)

    End Sub
    ''' <summary>
    ''' 读取点文件数据
    ''' </summary>
    ''' <param name="pointDateFileName">点文件坐标名称</param>
    ''' <param name="result">返回的点坐标数据数组</param>
    ''' <param name="hasRecordCountHead">数据行是否包含点个数文件头 true 包含</param>
    ''' <remarks></remarks>
    Public Sub readPointData(ByVal pointDateFileName As String, ByRef result(,) As String, Optional ByVal hasRecordCountHead As Boolean = False)
        If (Dir(pointDateFileName) = "") Then
            loger.log("点数据文件" & pointDateFileName & "不存在", ClassConsManager.LOG_LEVEL_ERROR)
        End If
        Try
            Dim fileReader = My.Computer.FileSystem.OpenTextFileReader(pointDateFileName, System.Text.Encoding.Default)
            Dim pointCount As Integer = 0
            If hasRecordCountHead Then
                pointCount = Val(fileReader.ReadLine())
            Else
                pointCount = 1000
            End If


            ReDim pointArray(pointCount, 10)

            Dim i = 0
            While (Not fileReader.EndOfStream)
                Dim dataLine As String = fileReader.ReadLine()

                If dataLine = "" Then
                    Continue While
                ElseIf Mid(dataLine, 1, 1) = "#" Then
                    Continue While
                End If

                Dim dataArrat = Split(dataLine, ClassConsManager.FIELD_SPERATE_STRING)
                '判断改点是否存在
                '点编号
                If ClassConsManager.FILE_NODE_NO_FIELD = -1 Then
                    pointArray(i, ClassConsManager.NODE_NO_FIELD) = Trim(Str(i))
                Else
                    pointArray(i, ClassConsManager.NODE_NO_FIELD) = dataArrat(ClassConsManager.FILE_NODE_NO_FIELD)
                End If
                '点名称
                If ClassConsManager.FILE_NODE_NAME_FIELD = -1 Then
                    pointArray(i, ClassConsManager.NODE_NAME_FIELD) = Trim(Str(i))
                Else
                    pointArray(i, ClassConsManager.NODE_NAME_FIELD) = dataArrat(ClassConsManager.FILE_NODE_NAME_FIELD)
                End If
                '经度
                If ClassConsManager.FILE_NODE_LONGTITUDE_FIELD = -1 Then
                    pointArray(i, ClassConsManager.NODE_LONGTITUDE_FIELD) = 0
                Else
                    If ZuoBiaoGeShi = ZB_DATA_FORMAT.DMS Then
                        pointArray(i, ClassConsManager.NODE_LONGTITUDE_FIELD) = DEG(dataArrat(ClassConsManager.FILE_NODE_LONGTITUDE_FIELD))
                    Else
                        pointArray(i, ClassConsManager.NODE_LONGTITUDE_FIELD) = (dataArrat(ClassConsManager.FILE_NODE_LONGTITUDE_FIELD))
                    End If
                End If

                '纬度
                If ClassConsManager.FILE_NODE_LAIITUDE_FIELD = -1 Then
                    pointArray(i, ClassConsManager.NODE_LAIITUDE_FIELD) = 0
                Else
                    If ZuoBiaoGeShi = ZB_DATA_FORMAT.DMS Then
                        pointArray(i, ClassConsManager.NODE_LAIITUDE_FIELD) = DEG(dataArrat(ClassConsManager.FILE_NODE_LAIITUDE_FIELD))
                    Else
                        pointArray(i, ClassConsManager.NODE_LAIITUDE_FIELD) = (dataArrat(ClassConsManager.FILE_NODE_LAIITUDE_FIELD))
                    End If
                End If

                '高程
                If ClassConsManager.FILE_NODE_ALTITUDE_FIELD = -1 Then
                    pointArray(i, ClassConsManager.NODE_ALTITUDE_FIELD) = 0
                Else
                    pointArray(i, ClassConsManager.NODE_ALTITUDE_FIELD) = dataArrat(ClassConsManager.FILE_NODE_ALTITUDE_FIELD)
                End If
                i = i + 1

            End While
            fileReader.Close()
            'If hasRecordCountHead = False Then
            '    '如果不包含数据文件个数，则需要将数组缩小。
            '    ReDim Preserve pointArray(i - 1 * 10)
            'End If
            If result Is Nothing Then
                result = pointArray
            End If
            Array.Copy(pointArray, result, (i - 1) * 10)
        Catch

        End Try

    End Sub

    Public Function getPointArray() As String(,)
        Return pointArray
    End Function

    Public Function getLineArray() As String(,)
        Return lineArray
    End Function
    Public Function DEG(ByVal m As Double)
        '度分秒转化为度小数
        Dim D, MI, S As Double
        D = Int(m)
        MI = Int((m - D) * 100)
        S = ((m - D) * 100 - MI) * 100
        DEG = D + MI / 60 + S / 3600
    End Function
End Class
